﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS  Domestic ClusterManagementHelper
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-1-30
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    /// <summary>
    /// Cluster Management模块数据库访问类
    /// </summary>
    public static class ClusterManagementHelper
    {
        /// <summary>
        /// 通过ClusterID查询Provinces
        /// </summary>
        public static IEnumerable GetProvincesWithClusterRelation(int clusterID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var cluster = dc.Clusters.FirstOrDefault(p => p.ClusterID == clusterID);
                var provinces = dc.Provinces.Where(p => !p.Disabled).ToArray();
                cluster.Provinces.Load();
                var source = from p in provinces
                             select new
                             {
                                 p.ProvinceID,
                                 p.Name,
                                 p.Description,
                                 ContainsCluster = cluster.Provinces.Contains(p) ? "checked" : ""
                             };
                return source.ToArray();
            }
        }
              
        /// <summary>
        /// 为Clusters添加Provinces
        /// </summary>
        public static void OnClusterInserting(ObjectContext dc, Cluster cluster, string provinceNames)
        {

            var provinces = dc.CreateQuery<Province>("[Provinces]").Where(p => p.Disabled == false);

            if (!String.IsNullOrEmpty(provinceNames))
            {

                foreach (var province in provinceNames.Split(','))
                {
                    int provinceID = int.Parse(province);

                    var provinceInCluster = provinces.First(p => p.ProvinceID == provinceID);

                    cluster.Provinces.Add(provinceInCluster);

                }

            }

        }

        /// <summary>
        /// 为Clusters修改Provinces
        /// </summary>
        public static void OnClusterUpdating(ObjectContext dc, Cluster cluster, string provinceNames)
        {
            cluster.Provinces.Load();
            cluster.Provinces.Clear();

            OnClusterInserting(dc, cluster, provinceNames);

        }


        /// <summary>
        /// 将一个cluster的激活状态设为未激活
        /// </summary>
        public static void SetClusterDisabled(int clusterID)
        {
            using (var dc = new CCEntities())
            {
                dc.Clusters.FirstOrDefault(p => p.ClusterID == clusterID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前ID获取Name
        /// </summary>
        public static IEnumerable getName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.Clusters.FirstOrDefault(p => p.ClusterID == entityID).Name;
                return name;
            }
        }
    }
}
